__author__ = 'shawhen'

import ctypes


class MainWindow(object):
    def __init__(self, app):
        from ...platform import what as m_what_platform

        super(MainWindow, self).__init__()

        self._app = app

        if "win" in m_what_platform.running_os["type"]:
            import datetime

            from ...cstd import stddef

            from ...sysapi.win32 import WinUser
            from ...sysapi.win32 import WinBase
            from ...sysapi.win32 import WinGDI

            self._wndclass = WinUser.WNDCLASSEX()
            self._wndclass.cbSize = ctypes.sizeof(WinUser.WNDCLASSEX)
            self._wndclass.style = WinUser.CS_HREDRAW | WinUser.CS_VREDRAW
            self._wndclass.lpfnWndProc = WinUser.WNDPROC(self._app._wnd_proc)
            self._wndclass.cbClsExtra = 0
            self._wndclass.cbWndExtra = 0
            self._wndclass.hInstance = WinBase.GetModuleHandle()
            self._wndclass.hIcon = WinUser.LoadImage(self._wndclass.hInstance, WinUser.IDI_APPLICATION,
                                                     WinUser.IMAGE_ICON, 0, 0, WinUser.LR_DEFAULTSIZE)
            self._wndclass.hCursor = WinUser.LoadImage(self._wndclass.hInstance, WinUser.IDC_ARROW,
                                                       WinUser.IMAGE_CURSOR, 0, 0, WinUser.LR_DEFAULTSIZE)
            self._wndclass.hbrBackground = WinGDI.GetStockObject(WinGDI.WHITE_BRUSH)
            self._wndclass.lpszMenuName = stddef.NULL
            self._wndclass.lpszClassName = str(datetime.datetime.now())

            WinUser.RegisterClassEx(self._wndclass)

            self._hWnd = WinUser.CreateWindowEx(0, self._wndclass.lpszClassName, "Demo Window",
                                                WinUser.WS_OVERLAPPEDWINDOW,
                                                WinUser.CW_USEDEFAULT, WinUser.CW_USEDEFAULT,
                                                WinUser.CW_USEDEFAULT, WinUser.CW_USEDEFAULT,
                                                None, None, self._wndclass.hInstance, None)

            self._app._register_window(self)
        else:
            raise NotImplementedError

    def show(self, active=True, how="normal", force=False):
        """
        :param active 是否激活窗口
        :param how 如何显示窗口
            "normal": 正常显示
            "min" or "miniminze": 最小化显示
            "max" or "maximinze": 最大化显示
            "hide": 隐藏
            "restore": 还原
            "show": 就只是显示而已
            "default": 以默认进程的尺寸显示
        :param force 强制
        """
        from ...platform import what as m_what_platform

        if "win" in m_what_platform.running_os["type"]:
            from ...sysapi.win32 import WinUser

            if how.startswith("hide"):
                WinUser.ShowWindow(self._hWnd, WinUser.SW_HIDE)
            elif how.startswith("default"):
                WinUser.ShowWindow(self._hWnd, WinUser.SW_SHOWDEFAULT)
            else:
                if force is True:
                    if how.startswith("min"):
                        WinUser.ShowWindow(self._hWnd, WinUser.SW_FORCEMINIMIZE)
                    else:
                        raise ValueError("unknown show args: active:{0}, how:{1}, force:{2}".format(active, how, force))
                else:
                    if active is True:
                        if how.startswith("min"):
                            WinUser.ShowWindow(self._hWnd, WinUser.SW_SHOWMINIMIZED)
                        elif how.startswith("max"):
                            WinUser.ShowWindow(self._hWnd, WinUser.SW_SHOWMAXIMIZED)
                        elif how.startswith("normal"):
                            WinUser.ShowWindow(self._hWnd, WinUser.SW_SHOWNORMAL)
                        elif how.startswith("show"):
                            WinUser.ShowWindow(self._hWnd, WinUser.SW_SHOW)
                    else:
                        if how.startswith("min"):
                            WinUser.ShowWindow(self._hWnd, WinUser.SW_SHOWMINNOACTIVE)
                        elif how.startswith("max"):
                            WinUser.ShowWindow(self._hWnd, WinUser.SW_MAXIMIZE)
                        elif how.startswith("normal"):
                            WinUser.ShowWindow(self._hWnd, WinUser.SW_SHOWNOACTIVATE)
                        elif how.startswith("show"):
                            WinUser.ShowWindow(self._hWnd, WinUser.SW_SHOWNA)
        else:
            raise NotImplementedError

    def update(self):
        from ...platform import what as m_what_platform

        if "win" in m_what_platform.running_os["type"]:
            from ...sysapi.win32 import WinUser

            return WinUser.UpdateWindow(self._hWnd)
        else:
            raise NotImplementedError

    def _handle(self):
        return self._hWnd